package middleware

import (
	"github.com/gin-gonic/gin"
	"go_auth/ext/app"
	"go_auth/ext/e"
	"go_auth/servers/users"
	"go_auth/utils/commons"
	"net/http"
)

func JWT() gin.HandlerFunc {
	return func(c *gin.Context) {
		appG := app.Gin{C: c}
		//cookie涉及到跨域问题
		token := c.Query("token")
		uri := c.Request.URL.Path
		method := c.Request.Method

		filter := commons.StringIsEmpty(token, 32)

		if !filter {
			appG.CheckResponse(http.StatusOK, e.TokenError, nil)
			return
		}

		code := users.CheckToken(token, uri, method)
		if code != e.Success {
			appG.CheckResponse(http.StatusOK, code, nil)
			return
		}
		c.Next()
	}
}
